/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.ProjectIndex;
import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.service.BiReportService;
import com.wolfpire.proms.service.ProjectIndexService;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;

/**
 * 报表控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/bireport")
public class BiReportController {
	
	@Resource BiReportService biReportService;
	
	@Resource ProjectIndexService projectIndexService;
	
	@Resource TaskService taskService;
	
	/**
	 * 任务增加量
	 * @param portal
	 * @return
	 */
	@RequestMapping("/taskInc")
	public ModelAndView taskInc(@RequestParam(value = "portal", required = false) String portal) {
		ModelAndView mv = new ModelAndView("proms/bireport/task_inc");
		mv.getModel().put("portal", !StringUtils.isBlank(portal) ? portal : StringUtils.EMPTY);
		return mv;
	}
	
	/**
	 * 任务数量统计
	 * @param portal
	 * @return
	 */
	@RequestMapping("/taskCount")
	public ModelAndView taskCount(@RequestParam(value = "portal", required = false) String portal) {
		ModelAndView mv = new ModelAndView("proms/bireport/task_count");
		if (!StringUtils.isBlank(portal)) {
			mv = new ModelAndView("proms/bireport/portal/task_count");
		}
//		mv.getModel().put("portal", !StringUtils.isBlank(portal) ? portal : StringUtils.EMPTY);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/taskCountData")
	public Result taskCountData(Long projectIndexId) {
		Result result = new Result();
		try {
			result.setSuccess(true);
			Map<String, Object> data = new HashMap<String, Object>();
			List<Map<String, Object>> lists = this.biReportService.findTaskCount(projectIndexId);
			data.put("piId", projectIndexId);
			ProjectIndex pi = this.projectIndexService.get(projectIndexId);
			data.put("ppiId", pi.getParentId());
			data.put("name", pi.getName());
			data.put("leaf", pi.isLeaf());
			data.put("data", lists);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("加载出错，请联系管理员！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/taskCountList")
	public EasyuiPagination<Map<String, Object>> taskCountList(
			EasyuiPage<Map<String, Object>> page,
			@RequestParam(value = "projectIndexId", required = true) Long projectIndexId,
			@RequestParam(value = "type", required = false) String type) {
		Page<Map<String, Object>> tasks = this.biReportService.setTaskCountList(page, projectIndexId, type);
		return new EasyuiPagination<Map<String, Object>>(tasks);
	}
	
	/**
	 * 资金使用情况
	 * @param portal
	 * @return
	 */
	@RequestMapping("/amountUse")
	public ModelAndView amountUse(@RequestParam(value = "portal", required = false) String portal) {
		ModelAndView mv = new ModelAndView("proms/bireport/amount_use");
		if (!StringUtils.isBlank(portal)) {
			mv = new ModelAndView("proms/bireport/portal/amount_use");
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/amountUseData")
	public Result amountUseData(Long projectIndexId) {
		Result result = new Result();
		try {
			result.setSuccess(true);
			Map<String, Object> data = new HashMap<String, Object>();
			List<Map<String, Object>> lists = this.biReportService.findSumAmountUser(projectIndexId);
			data.put("piId", projectIndexId);
			ProjectIndex pi = this.projectIndexService.get(projectIndexId);
			data.put("ppiId", pi.getParentId());
			data.put("name", pi.getName());
			data.put("leaf", pi.isLeaf());
			data.put("data", lists);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("加载出错，请联系管理员！");
		}
		return result;
	}
	
	/**
	 * 资金投入情况
	 * @param portal
	 * @return
	 */
	@RequestMapping("/amountInput")
	public ModelAndView amountInput(@RequestParam(value = "portal", required = false) String portal) {
		ModelAndView mv = new ModelAndView("proms/bireport/amount_input");
		mv.getModel().put("portal", !StringUtils.isBlank(portal) ? portal : StringUtils.EMPTY);
		return mv;
	}
	
	/**
	 * 任务进度情况统计
	 * @param portal
	 * @return
	 */
	@RequestMapping("/taskProgress")
	public ModelAndView taskProgress(@RequestParam(value = "portal", required = false) String portal) {
		ModelAndView mv = new ModelAndView("proms/bireport/task_progress");
		if (!StringUtils.isBlank(portal)) {
			mv = new ModelAndView("proms/bireport/portal/task_progress");
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/taskProgressData")
	public Result taskProgressData(Long projectIndexId) {
		Result result = new Result();
		try {
			result.setSuccess(true);
			Map<String, Object> data = new HashMap<String, Object>();
			List<Map<String, Object>> lists = this.biReportService.findCountTaskProgress(projectIndexId);
			data.put("piId", projectIndexId);
			ProjectIndex pi = this.projectIndexService.get(projectIndexId);
			data.put("ppiId", pi.getParentId());
			data.put("name", pi.getName());
			data.put("leaf", pi.isLeaf());
			data.put("data", lists);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("加载出错，请联系管理员！");
		}
		return result;
	}
	
	/**
	 * 根据权重值统计
	 * @param portal
	 * @return
	 */
	@RequestMapping("/weightProgress")
	public ModelAndView weightProgressData(@RequestParam(value = "portal", required = false) String portal) {
		ModelAndView mv = new ModelAndView("proms/bireport/weight_progress");
		if (!StringUtils.isBlank(portal)) {
			mv = new ModelAndView("proms/bireport/portal/weight_progress");
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/weightProgressData")
	public Result weightProgressData(Long projectIndexId) {
		Result result = new Result();
		try {
			result.setSuccess(true);
			Map<String, Object> data = new HashMap<String, Object>();
			List<Map<String, Object>> lists = this.biReportService.findWeightProgressData(projectIndexId);
			data.put("piId", projectIndexId);
			ProjectIndex pi = this.projectIndexService.get(projectIndexId);
			data.put("ppiId", pi.getParentId());
			data.put("name", pi.getName());
			data.put("leaf", pi.isLeaf());
			data.put("data", lists);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("加载出错，请联系管理员！");
		}
		return result;
	}
	
	@RequestMapping("/tasks")
	public ModelAndView tasks(Long projectIndexId) {
		return new ModelAndView("proms/bireport/task_list", "projectIndexId", projectIndexId);
	}
	
	@ResponseBody
	@RequestMapping("/queryTasks")
	public EasyuiPagination<Map<String, Object>> queryTasks(
			EasyuiPage<Map<String, Object>> page,
			Task filterTask) {
		Page<Map<String, Object>> tasks = this.taskService.setPageMyDataList(page, filterTask);
		return new EasyuiPagination<Map<String, Object>>(tasks);
	}
	
	@RequestMapping("/projectIndexMonitor")
	public ModelAndView projectIndexMonitor() {
		return new ModelAndView("proms/bireport/projectindex_monitor");
	}
	
	@ResponseBody
	@RequestMapping("/projectIndexMonitorData")
	public EasyuiPagination<Map<String, Object>> projectIndexMonitorData(EasyuiPage<Map<String, Object>> page, Long projectIndexId, String startTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("projectIndexId", projectIndexId);
		if (StringUtils.isBlank(startTime)) {
			params.put("startTime", startTime);
		}
		if (StringUtils.isBlank(endTime)) {
			params.put("endTime", endTime);
		}
		Page<Map<String, Object>> projectIndexMonitors = this.biReportService.setProjectIndexMonitorDataList(page, params);
		return new EasyuiPagination<Map<String, Object>>(projectIndexMonitors);
	}
	
}
